package com.example.courtcounter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.w3c.dom.Element;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;

public class NetActivity2 extends AppCompatActivity implements Runnable {
    //实现runnable接口implement Runnable
    private static final String TAG = "Net";
    Handler handler;
    TextView show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net2);
        show = findViewById(R.id.html_out);

        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 7) {
                    String str = (String) msg.obj;
                    //收到子线程返回的数据
                    Log.i(TAG, "handleMessage: 收到str=" + str);
                    //Toast.makeText(NetActivity2.this,str,Toast.LENGTH_SHORT);
                    show.setText(str);
                }
                super.handleMessage(msg);
            }
        };
    }
        public void onClick(View btn){
            Log.i(TAG,"onClick:");
            Thread t = new Thread(this);
            Log.i(TAG, "onCreate:启动子线程");
            t.start();//this.run()
        }

        public void run() {
            Log.i(TAG, "run:线程已运行");
            //可访问网络资源
            String retStr = "";
            try {
                Log.i(TAG, "run:正在工作。。。。。");

                Document doc = Jsoup.connect("https://chl.cn/?jinri").get();
                Log.i(TAG,"title=" + doc.title());
                Elements tables = doc.getElementsByTag("table");
                org.jsoup.nodes.Element table1 = tables.first();
                Elements rows = table1.getElementsByTag("tr");
                for(org.jsoup.nodes.Element row : rows) {
                    Elements tds = row.getElementsByTag("td");
                    org.jsoup.nodes.Element td1 = tds.first();
                    org.jsoup.nodes.Element td2 = tds.get(4);
                    Log.i(TAG, td1.text() + "价格：" + td2.text());

                    retStr += (td1.text() + "=>" + td2.text() + "\n");
                }


            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            //获取Msg对象，用于返回主线程等（发送数据给主线程 ）
            Message msg = handler.obtainMessage(7);
            //msg.what = 7;
            msg.obj = retStr;
            handler.sendMessage(msg);
        }

    ///把输入的转化为字符串
    private String inputStream2String(InputStream inputStream)
            throws IOException{
        final int bufferSize = 1024;
        final char[] buffer = new char[bufferSize];
        final StringBuilder out =new StringBuilder();
        Reader in =new InputStreamReader(inputStream,"utf-8");
        while (true) {
            int rsz = in.read(buffer, 0, buffer.length);
            if (rsz <0)
                break;
            out.append(buffer, 0, rsz);
        }
        return out.toString();
    }

}